<?php
// This class will generate module skeleton
class HModuleRobot {		
	function generate($module_name) {		
		$data = HSql::fetch2Array(HSql::getData('portal_modules',"WHERE module_name = '".trim($module_name)."'"));		
		$module_path = FRONT_MODULE_PATH.SEPARATOR.$module_name;
		
		if (count($data) == 0) {	
			// Insert Module into Database using HModuleAdmin
			HModuleAdmin::insert($module_name);						
			
			// Create Module Folder
			mkdir($module_path);
			
			// Create index.php
			$fp = fopen($module_path.SEPARATOR.'index.php','w+');
			self::writeCodeBehind($fp, $module_name);
			
			// Create index.html.php
			$fp = fopen($module_path.SEPARATOR.'index.html.php','w+');
			
			return true;
		}
		else {
			return false;
		}
	}
	
	function writeCodeBehind($fp, $module_name) {
		define('TAB', '  ');
		fprintf($fp,"<?php \n");
		fprintf($fp,"class index extends HModule { \n");	
		fprintf($fp,TAB."function run() { \n");	
		fprintf($fp,TAB.TAB."echo 'Module ".$module_name." has been created successfully!'; \n");
		fprintf($fp,TAB."} \n");
		fprintf($fp,"} \n");
	}
	
	function generateViewFile($viewfile, $mid) {
		$a = HSql::fetch2Array(HSql::getData('portal_modules',"WHERE id = '$mid'"));
		$module_name = $a[0]['module_name'];
		
		$module_path = FRONT_MODULE_PATH.SEPARATOR.$module_name;		
		// Create index.php
		$fp = fopen($module_path.SEPARATOR.$viewfile.'.php','w+');
		define('TAB', '  ');
		fprintf($fp,"<?php \n");
		fprintf($fp,"class ".$module_name.'_'.$viewfile." extends HModule { \n");	
		fprintf($fp,TAB."function run() { \n");	
		fprintf($fp,TAB.TAB."echo 'Module ".$module_name." has been created successfully!'; \n");
		fprintf($fp,TAB."} \n");
		fprintf($fp,"} \n");
		
		// Create index.html.php
		$fp = fopen($module_path.SEPARATOR.$viewfile.'.html.php','w+');
	}
}